#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
Python 3.x
Flask 2.1.3
Flask-Limiter 2.6.3

Official Manual Address : https://flask-limiter.readthedocs.io/en/stable/index.html

Custom Key Function
"""
import GeoIP
from flask import Flask, request
from flask_limiter import Limiter
from flask_login import current_user  # 登入系统的用户

app = Flask(__name__)

limiter = Limiter()
limiter.init_app(app=app)


@app.route("/my_route")
@limiter.limit("10/hour", key_func=lambda: current_user.username)  # 速率限制：每小时10次访问
def user_name():
    """
    每个用户速率限制
    :return:
    """
    return "USER NAME"


gi = GeoIP.open("GeoLiteCity.dat", GeoIP.GEOIP_INDEX_CACHE | GeoIP.GEOIP_CHECK_CACHE)


def get_request_country():
    """
    城市请求
    :return:
    """
    return gi.record_by_name(request.remote_addr)['region_name']


@limiter.limit("10/hour", key_func=get_request_country)  # 速率限制：每小时10次访问
def country():
    """
    每个城市速率限制
    :return:
    """
    return "COUNTRY"import GeoIP
from flask import Flask, request
from flask_limiter import Limiter
from flask_login import current_user  # 登入系统的用户

app = Flask(__name__)

limiter = Limiter()
limiter.init_app(app=app)


@app.route("/my_route")
@limiter.limit("10/hour", key_func=lambda: current_user.username)  # 速率限制：每小时10次访问
def user_name():
    """
    每个用户速率限制
    :return:
    """
    return "USER NAME"


gi = GeoIP.open("GeoLiteCity.dat", GeoIP.GEOIP_INDEX_CACHE | GeoIP.GEOIP_CHECK_CACHE)


def get_request_country():
    """
    城市请求
    :return:
    """
    return gi.record_by_name(request.remote_addr)['region_name']


@limiter.limit("10/hour", key_func=get_request_country)  # 速率限制：每小时10次访问
def country():
    """
    每个城市速率限制
    :return:
    """
    return "COUNTRY"
